package com.mvp.shq.washer.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.utils.ImageUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/6.
 */

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.MyViewHolder> {

    public static final String DEFAULT_IMG = "DEFAULT_IMG";
    private Context context;
    private List<String> imgs;
    private int EVENT_CODE;
    private ImageUtil imageUtil;
    public ImageAdapter(Context mContext,List<String> list,int code){
        context = mContext;
        imgs = list;
        EVENT_CODE = code;
        imageUtil = new ImageUtil(context);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (imgs.get(position).equals(DEFAULT_IMG)){
            imageUtil.loadResImage(R.mipmap.feedback_pic_add,holder.img);
        }else {
            imageUtil.loadLocalImage(imgs.get(position),holder.img);
        }
        holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxBus.get().send(EVENT_CODE,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return imgs.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img)
        ImageView img;
        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
